package com.intelligent.bot.api.sys.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.bot.base.result.B;
import com.intelligent.bot.constant.CommonConst;
import com.intelligent.bot.model.AdConfig;
import com.intelligent.bot.model.KeyNumberConfig;
import com.intelligent.bot.model.SysConfig;
import com.intelligent.bot.model.base.BasePageHelper;
import com.intelligent.bot.model.req.sys.admin.SysConfigUpdateReq;
import com.intelligent.bot.model.res.sys.admin.SysConfigQueryRes;
import com.intelligent.bot.model.res.sys.admin.SysConfigUploadImgRes;
import com.intelligent.bot.service.keyConfig.KeyNumberConfigService;
import com.intelligent.bot.service.sys.AdConfigService;
import com.intelligent.bot.service.sys.ISysConfigService;
import com.intelligent.bot.utils.sys.ImgUtil;
import com.intelligent.bot.utils.sys.RedisUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;


@RestController
@RequestMapping("/sys/config")

public class SysConfigController {


    @Resource
    ISysConfigService payConfigService;

    @Resource
    KeyNumberConfigService keyNumberConfigService;
    @Resource
    AdConfigService adConfigService;

    @RequestMapping(value = "/queryPage", name = "查询payConfig", method = RequestMethod.POST)
    public B<SysConfigQueryRes> querySysConfig() {
        return payConfigService.queryPage();
    }

    @RequestMapping(value = "/queryKey", name = "查询keyConfig",method = RequestMethod.POST)
    public B<KeyNumberConfig> queryKeyConfig() {
        return keyNumberConfigService.queryPage();
    }
    @RequestMapping(value = "/updateKey",name = "更新keyConfig", method = RequestMethod.POST)
    public B<Void> update(@Validated @RequestBody KeyNumberConfig req) {
        return keyNumberConfigService.update(req);
    }

    @RequestMapping(value = "/queryAd", name = "查询adConfig",method = RequestMethod.POST)
    public B<Page<AdConfig>> queryAdConfig(@Validated @RequestBody BasePageHelper req) {
        return adConfigService.queryPage(req);
    }
    @RequestMapping(value = "/updateAd",name = "更新adConfig", method = RequestMethod.POST)
    public B<Void> updateAdConfig(@Validated @RequestBody AdConfig req) {
        return adConfigService.update(req);
    }
    @RequestMapping(value = "/addAd",name = "增加adConfig", method = RequestMethod.POST)
    public boolean addAdConfig(@Validated @RequestBody AdConfig req) {
        return adConfigService.save(req);
    }
    @RequestMapping(value = "/removeAd",name = "删除adConfig", method = RequestMethod.POST)
    public boolean removeAdConfig(@Validated @RequestBody AdConfig req) {
        return adConfigService.removeById(req.getAdId());
    }
    @RequestMapping(value = "/update", name = "更新payConfig", method = RequestMethod.POST)
    public B<Void> update(@Validated @RequestBody SysConfigUpdateReq req) {
        return payConfigService.update(req);
    }

    @RequestMapping(value = "/upload/img", name = "上传图片")
    public B<SysConfigUploadImgRes> uploadImg(MultipartFile file) throws IOException {
        String oldFileName = Objects.requireNonNull(file.getOriginalFilename()).substring(0, file.getOriginalFilename().lastIndexOf("."));
        String fileName = ImgUtil.uploadMultipartFile(file, oldFileName);
        SysConfig cacheObject = RedisUtil.getCacheObject(CommonConst.SYS_CONFIG);
        SysConfigUploadImgRes res = SysConfigUploadImgRes.builder()
                .imageUrl(cacheObject.getIsOpenOss() == 0 ? cacheObject.getImgReturnUrl() + fileName : fileName)
                .filePatch(fileName).build();
        return B.okBuild(res);
    }
}
